package com.delivery.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.*;


@Configuration
public class MyThreadConfig {
    @Autowired
    private ThreadPoolConfigProperties pool;
        @Bean(name = "taskExecutor")
        public Executor asyncExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(pool.getCoreSize());
            executor.setMaxPoolSize(pool.getMaxSize());
            executor.setKeepAliveSeconds(pool.getKeepAliveTime());
            executor.setQueueCapacity(pool.getQueueCapacity());
            executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
            executor.initialize();
            return executor;
        }

}
